//var organ = window.location.host;
var oragn = 'www.hitebaas.com';
//var organ = '118.178.255.202';
$(function () {
var  name;
  $.ajax({
    type: 'post',
    url: 'https://www.hitebaas.com:8100/baas-platform/queryenterprises',
	xhrFields:{ withCredentials: true },
    data: {},
    success: function (resp) {
      console.log(resp.content);
      var tag_data = resp.content;
      $('#selectPage2').selectPage({
        showField: 'name',
        keyField: 'id',
        //设置每页显示记录数
        pageSize: 5,
        data: tag_data,
        //关闭向下的三角尖按钮
        dropButton: false,
        //格式化显示项目，提供源数据进行使用
        formatItem: function (data) {
        	
          return data.enterpriseName;
        }
      });
    }
  });
});
var keys = [];//收集id
var ekeys = [];//收集id
var headers = [];//背景
//企业改变回调函数。
var qychange = function (obj) {
  console.log(obj.value);
  var enterpriseId = $("#selectPage2").val();
  keys = [];
  html = [];//重置key
  $(".temp").remove();//重置表单
  $(".gsjj").remove();//重置表单
  $("#datapanel").html('');
  //取查询表单
  $.ajax({
    type: 'post',
    url: 'https://' + organ + ':8100/baas-platform/qydataheaders',
	xhrFields:{ withCredentials: true },
    data: JSON.stringify({
      "tradeName": enterpriseId
    }),
    contentType: 'application/json;charset=utf-8',
    success: function (data) {
      console.log(data);
      var html = [];
      headers = [];
      headers.push('<div class="jiaoyi-item">');
      for (var i = 0; i < data.length; i++) {
        var kv = data[i];
        ekeys.push(kv.key);//收集表单id
		keys.push(kv.ekey);//收集表单id
        html.push('<div class="temp">');
        html.push('<span>' + kv.value + '：</span>');
        html.push('<input id="' + kv.ekey + '" type="text" placeholder="请输入' + kv.value + '">');
        html.push('</div>');

        //-----------------------------------------------------------------
        headers.push('<div>' + kv.value + '</div>');
      }
      headers.push('</div>');
      //生成查询面板

      $(".temp").remove();
      $(".gsjj").remove();//重置表单
      $("#querypanel").append(html.join(''));
      if (obj.value == "贵州省铁塔智慧楼宇云平台1") {
        $("#querypanel").before('<div class="gsjj"> <img src="./images/team-03.png "/> <div class="gsjj2"><p>福建中科星泰（SyncTech）数据科技有限公司</p> <p>福建中科星泰（SyncTech）数据科技有限公司是一家专业从事软件开发、数据应用软件开发、数据采集、软件定制、软件实施的高新技术企业。，长期从事软件开发与定制的专业人才；具有雄厚的技术开发实力，全方位满足政府与企业信息化需求。公司非常重视企业的内部管理工作，市场销售、软件研发、技术支持是公司的三大核心部门，现已经建立了一套比较完善的管理体制。在客户服务方面，本着为客户服务的思想，设立了产品售后咨询电话</p> </div> </div>');
        $('#.369').val('');
        // $('.sp_container').html('')
      }

      //生成表头
      $("#datapanel").html('');
      $("#datapanel").append(headers.join(''));

      
       console.log(html);
    }
    
  });
  
}

//查询方法
var params = {
  "index": 0,
  "size": 4,
  "tradeName": "",
  "list": []
};
var queryDatas = function () {
	
  var enterpriseId = $("#selectPage2").val();
  params.tradeName = enterpriseId;
  params.list = [];
 // alert(keys.length);
  for (var i = 0; i < keys.length; i++) {
	  
    var id = keys[i];
    var v = $("#" + id).val();
	//alert("v:"+v);
	//alert("id:"+id);
    var kv = {
      "key": id,
      "value": v
    };
    params.list.push(kv);
  }
  renderPaging();
  return;
  $.ajax({
    type: 'post',
    url: 'https://' + organ + ':8100/baas-platform/qydata',
	xhrFields:{ withCredentials: true },
    data: JSON.stringify(params),
    contentType: 'application/json;charset=utf-8',
    success: function (data) {
      var rows = data.data;
      var rowObjects = [];

      
      for (var i = 0; i < rows[0].length; i++) {
        var row = rows[i];
        var rowObject = {};
        for (var j = 0; j < row.length; j++) {
          var key = row[j].key;
          var value = row[j].value;
          rowObject[key] = value;
        }
        rowObjects.push(rowObject);
      }
      //拼接html
      var htmls = [];
      for (var i = 0; i < rowObjects.length; i++) {
        var rowObject = rowObjects[i];
        htmls.push('<div class="jiaoyi-item">');
        for (var j = 0; j < keys.length; j++) {
          var col = keys[j];
          if (col == 'hash') {
            htmls.push('<div title="' + rowObject[col] + '"> <a href="javascript:void(0);">' + rowObject[col].replace(/\ +/g, "") + '</a></div>');
          } else {
            htmls.push('<div  title="' + rowObject[col] + '">' + rowObject[col].replace(/\ +/g, "") + '</div>');
          }
        }
        htmls.push('</div>');
      }
      $("#datapanel").append(htmls.join(''));
    }
  });
}
var renderPaging = function () {
  var enterpriseId = $("#selectPage2").val();
  var oPage = {
    search: '', // 搜索条件
    current: 1, // 当前页
    size: 4,	// 每页显示多少条
    paging: null, // 分页对象

    init: function () {
      this.setPaging(); // 初始化分页对象并调用loadData方法

      this.initEvent(); // 绑定事件
    },
    // 初始化事件
    initEvent: function () {
      var _this = this;

      // 调到第一页点击
      $('.jumpBtn').on('click', function () {
        _this.paging.setCurrent(1);
      });

      // 搜索按钮点击
      $('#searchBtn').on('click', function () {
        _this.search = $('#searchIpt').val();

        // 设置当前页，会触发jump方法
        _this.paging.setCurrent(1);
      });
    },

    // 发送ajax获取数据
    loadData: function () {
      var _this = this;
      params.enterpriseId = enterpriseId;
      $.ajax({
        type: 'post',
        url: 'https://' + organ + ':8100/baas-platform/qydata',
		xhrFields:{ withCredentials: true },
        data: JSON.stringify(params),
        contentType: 'application/json;charset=utf-8',
        success: function (res) {
          // 设置tbody的html
          _this.setTbody(res.data);
          // 必须调用 设置分页对象总页数 -------------------------------------------------
          _this.paging.setTotal(res.count);
        }
      });

    },

    // 创建分页对象
    setPaging: function () {
      var _this = this;
      this.paging = new MyPaging('#box2', {
        size: this.size,
        sizes: [1, 2, 4, 10, 20, 100],	// 选择每页条数
        total: 0,
        current: this.current,
        prevHtml: '上一页',
        nextHtml: '下一页',
        layout: 'total, totalPage, sizes, prev, pager, next, jumper',
        jump: function () {
          // 这儿的this指向paging对象
          params.index = this.current - 1; // 设置当前页
          params.size = this.size;

          _this.current = this.current; // 设置当前页
          _this.size = this.size; // 设置当前每页多少条

          // 点击页数按钮都会调用
          _this.loadData();
        }
      });
    },

    // 设置tbody的html
    setTbody: function (rows) {
      var rowObjects = [];
      for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        var rowObject = {};
        for (var j = 0; j < row.length; j++) {
          var key = row[j].key;
          var value = row[j].value;
          rowObject[key] = value;
        }
        rowObjects.push(rowObject);
      }
      //拼接html
      $("#datapanel").html('');
      var htmls = [];
      for (var i = 0; i < rowObjects.length; i++) {
        var rowObject = rowObjects[i];
        htmls.push('<div class="jiaoyi-item">');
        for (var j = 0; j < keys.length; j++) {
          var col = keys[j];
          if (col == 'hash') {
            htmls.push('<div  title="' + rowObject[col] + '"> <a href="javascript:void(0);">' + rowObject[col] + '</a></div>');
          } else {
            htmls.push('<div  title="' + rowObject[col] + '">' + rowObject[col] + '</div>');
          }
        }
        htmls.push('</div>');
      }
      $("#datapanel").append(headers.join('') + htmls.join(''));


    }
  }
  oPage.init();
}



